/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.PlannedMonthlyDemand;

import entity.PlannedMonthlyDemand;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import session.stateless.PlannedMonthlyDemandBeanLocal;
import session.stateless.MRP.SaleForecast.SaleForecastBeanLocal;

/**
 *
 * @author zhozho
 */
@ManagedBean
@ViewScoped
public class GenerateSalesOperationPlan implements Serializable{
    static public List<String> monthList;
    static public String[] monthName = {"January", "February",
                          "March", "April", "May", "June", "July",
                          "August", "September", "October", "November",
                          "December"};
    private String startMonth;
    private String endMonth;
    private List<PlannedMonthlyDemand> monthlyData;

    
    @EJB
    PlannedMonthlyDemandBeanLocal plannedMonthlyDemandBeanLocal;
    @EJB
    SaleForecastBeanLocal saleForecastBeanLocal;
    
            
    //HARD CODE DATA
    private List<PlannedMonthlyDemand> database = new ArrayList<PlannedMonthlyDemand>();  
        
    static {
        Calendar time = Calendar.getInstance();
        monthList = new ArrayList<String>();
        for (int i = 0; i < 12; ++i)
        {
            String currentMonth;
            currentMonth = monthName[time.get(Calendar.MONTH)];
            String currentYear;
            currentYear = Integer.toString(time.get(Calendar.YEAR));
            monthList.add(currentMonth + " " + currentYear);
            time.add(Calendar.MONTH, 1);
        } 
    }
    /** Creates a new instance of GenerateSalesOperationPlan */
    public GenerateSalesOperationPlan() {
        monthlyData = new ArrayList<PlannedMonthlyDemand>();
    }
    
    
    public void initView(ActionEvent event) {

        if (database.size() == 0) {
            database = plannedMonthlyDemandBeanLocal.getPlannedMonthlyDemands();
            
        }
        if (database.size() == 0) {
            database = new ArrayList<PlannedMonthlyDemand>();
            database.add(plannedMonthlyDemandBeanLocal.createPlannedMonthlyDemand("January 2012", null, null));
            database.add(plannedMonthlyDemandBeanLocal.createPlannedMonthlyDemand("February 2012", null, null));
            database.add(plannedMonthlyDemandBeanLocal.createPlannedMonthlyDemand("March 2012", null, null));
            database.add(plannedMonthlyDemandBeanLocal.createPlannedMonthlyDemand("April 2012", null, null));
            database.add(plannedMonthlyDemandBeanLocal.createPlannedMonthlyDemand("May 2012", null, null));
            database.add(plannedMonthlyDemandBeanLocal.createPlannedMonthlyDemand("June 2012", null, null));
            database.add(plannedMonthlyDemandBeanLocal.createPlannedMonthlyDemand("July 2012", null, null));
            database.add(plannedMonthlyDemandBeanLocal.createPlannedMonthlyDemand("August 2012", null, null));
            database.add(plannedMonthlyDemandBeanLocal.createPlannedMonthlyDemand("September 2012", null, null));
            database.add(plannedMonthlyDemandBeanLocal.createPlannedMonthlyDemand("October 2011", null, null));
            database.add(plannedMonthlyDemandBeanLocal.createPlannedMonthlyDemand("November 2011", null, null));
            database.add(plannedMonthlyDemandBeanLocal.createPlannedMonthlyDemand("December 2011", null, null));
        }
    }
    
    
    
    public List<String> getMonthList() {
        return monthList;
    }

    public void setMonthList(List<String> monthList) {
        this.monthList = monthList;
    }

    public List<PlannedMonthlyDemand> getMonthlyData() {
        return monthlyData;
    }

    public void setMonthlyData(List<PlannedMonthlyDemand> monthlyData) {
        this.monthlyData = monthlyData;
    }

    public String getEndMonth() {
        return endMonth;
    }

    public void setEndMonth(String endMonth) {
        this.endMonth = endMonth;
    }

    public String getStartMonth() {
        return startMonth;
    }

    public void setStartMonth(String startMonth) {
        this.startMonth = startMonth;
    }

        
    public void retrieveMonthlyData(ActionEvent event) {
        monthlyData.clear();
        //run from start month to end month
        int startIndex = monthList.indexOf(startMonth);
        int endIndex = monthList.indexOf(endMonth);
        
        for (int i = startIndex; i <= endIndex; i++)
        {
            monthlyData.add(plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i)));            
        }
    }
    
    public void refresh(ActionEvent event)
    {     
        int startIndex = monthList.indexOf(startMonth);
        int endIndex = monthList.indexOf(endMonth);
        
        for (int i = startIndex; i <= endIndex; i++)
        {

            Long previousInventory;
            if (i == 0){
                //inventory of the current month - 1
                
                //TESTING
                previousInventory = new Long(0);
            }
            else if (i - startIndex == 0) {
                previousInventory = plannedMonthlyDemandBeanLocal.findByMonth(monthList.get(i-1)).getInventory();
            }
            else {
                previousInventory = monthlyData.get(i-startIndex-1).getInventory();
            }
            monthlyData.get(i-startIndex).setInventory(plannedMonthlyDemandBeanLocal.calculateInventory(Long.parseLong(monthlyData.get(i-startIndex).getMonthlyForecast()), monthlyData.get(i-startIndex).getProductionPlan(), previousInventory));
        }        
    }
    
    public void generate(ActionEvent event)  throws IOException
    {
        refresh(null);
        for (int i = 0; i < monthlyData.size(); i++)
        {
            String month = monthlyData.get(i).getMonthName();
            int year = Integer.parseInt(month.substring(month.length() - 4));
            plannedMonthlyDemandBeanLocal.updatePlannedMonthlyDemand(monthlyData.get(i).getId(), Long.toString(saleForecastBeanLocal.getMonthlySaleForecasts(year, i, i).get(0).getTotalProductCaseQuantity()), monthlyData.get(i).getProductionPlan(), monthlyData.get(i).getInventory());
            //plannedMonthlyDemandBeanLocal.updatePlannedMonthlyDemand(monthlyData.get(i).getId(), monthlyData.get(i).getMonthlyForecast(), monthlyData.get(i).getProductionPlan(), monthlyData.get(i).getInventory());
        }
        
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("startMonth", monthlyData.get(0).getMonthName());
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("endMonth", monthlyData.get(monthlyData.size()-1).getMonthName());
        FacesContext.getCurrentInstance().getExternalContext().redirect("plannedMonthlyDemand.xhtml");
    }
}
